<?php

/*
	Data Access Object for Answer module
*/

/*require_once(FWS_DIR_MODEL.'answermodel.class.php');*/

class AnswerModelDao {

	function findAllAnswer() {
       	$resultat = mysql_query("SELECT ID_ANSWER, ID_EVENT, ID_PLAYER, ID_REF_ANSWER, COMMENTS FROM TBL_ANSWER ") or trigger_error(mysql_error(), E_USER_ERROR);
		$result = array();
       	while($row = mysql_fetch_array($resultat)) {
			$result[] = new AnswerModel($row);
   		}
		return $result;
	}
	
	function findRefAnswer() {
        $resultat = mysql_query("SELECT ID_ANSWER, ID_EVENT FROM TBL_ANSWER ORDER BY ID_EVENT") or trigger_error(mysql_error(), E_USER_ERROR);
		$result = array();
        while($row = mysql_fetch_array($resultat)) {
			$result[$row[0]] = $row[1]; 
       	}
		return $result;
	}

	function findRefTypAnswer() {
       	$resultat = mysql_query("SELECT ID_REF_ANSWER, LIB_ANSWER FROM REF_ANSWER ORDER BY GRP_ANSWER, LIB_ANSWER") or trigger_error(mysql_error(), E_USER_ERROR);
		$result = array();
        while($row = mysql_fetch_array($resultat)) {
			$result[$row[0]] = $row[1]; 
       	}
		return $result;
	}
	
	function queryAnswerById($id_Answer) {
		$query  = "SELECT E.ID_ANSWER, E.ID_EVENT, E.ID_PLAYER, E.ID_REF_ANSWER, ";
		$query .= "E.COMMENTS, R.LIB_TYP_ANSWER, P.FIRST_NAME ||' '||P.LAST_NAME AS PLAYER_NAME ";
		$query .= "FROM TBL_ANSWER E ";
		$query .= "INNER JOIN REF_ANSWER R ON R.ID_REF_ANSWER = E.ID_REF_ANSWER ";
		$query .= "INNER JOIN TBL_PLAYER P ON P.ID_PLAYER = E.ID_PLAYER ";
		$query .= "WHERE T.ID_ANSWER=".$id_Answer . " ORDER BY E.ID_REF_ANSWER ";
		$result = mysql_query ($query) or trigger_error(mysql_error(), E_USER_ERROR);
		$row = mysql_fetch_assoc($result);
		$Answer = new AnswerModel($row); 
		return $Answer;
	}

	function insertAnswer($answer) {
		$query  = "INSERT INTO TBL_ANSWER(ID_REF_ANSWER, ID_EVENT, COMMENTS, ID_PLAYER) "
		. "VALUES('" . $answer->getId_ref_answer() . "','" . $answer->getId_event() . "','"
		. $answer->getComments() . "','" . $answer->getId_player() . "')";
		$result = mysql_query ($query) or trigger_error(mysql_error(), E_USER_ERROR);
	}

}
?>